﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using LIM.controller;
using Telerik.WinControls.Data;
using Telerik.WinControls.UI;
using LIM.model;
using System.Linq;

namespace LIM.forms
{
    public partial class radForm_DanhMuc_DanhMucHeThong : Telerik.WinControls.UI.RadForm
    {
        public radForm_DanhMuc_DanhMucHeThong()
        {
            InitializeComponent();
            this.radTreeView_DanhMuc.TreeViewElement.AllowAlternatingRowColor = true;
        }
        DanhMucHeThongController controller = new DanhMucHeThongController();
        List<tbl_DanhMuc_HeThong> lstDMHT = new List<tbl_DanhMuc_HeThong>();
        private void radForm_CRM_DanhSachBaoGia_Load(object sender, EventArgs e)
        {
            LoadDM_HeThong();
            SetupTemplates("",-1);
            LoadRefresh();
        }

        private void SetupTemplates(string ABC,int Level)
        {
            #region
            using (this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.DeferRefresh())
            {
                this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.MasterTemplate.Reset();
                if (ABC == "")
                {
                    this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.DataSource = controller.LoadDataParent();
                }
                //else
                //{
                //    this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.DataSource = controller.Find(ABC);
                //}
                this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Columns["MaDanhMuc"].HeaderText = "Mã Danh mục";
                this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Columns["TenGoi"].HeaderText = "Tên gọi";
                this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Columns["GhiChu"].HeaderText = "Ghi chú";
                this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Columns["IsUsed"].HeaderText = "Đã sử dụng";
                for (int i = 0; i < this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.ColumnCount; i++)
                {
                    this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Columns[i].IsVisible = false;
                }
                this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Columns["MaDanhMuc"].IsVisible = true;
                this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Columns["MaDanhMuc"].Width = 80;
                this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Columns["TenGoi"].IsVisible = true;
                this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Columns["TenGoi"].Width = 220;
                this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Columns["GhiChu"].IsVisible = true;
                this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Columns["GhiChu"].Width = 130;
                this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Columns["IsUsed"].IsVisible = true;
                this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Columns["IsUsed"].Width = 80;

                #region
                /*
                this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.AutoGenerateHierarchy = true;

                for (int i = 0; i < this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].ColumnCount; i++)
                {
                    this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Columns[i].IsVisible = false;
                    if (this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Columns[i].Name == "MaDanhMuc")
                    {
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Columns[i].HeaderText = "Mã Danh mục";
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Columns[i].IsVisible = true;
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Columns[i].Width = 80; 
                    }
                    if (this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Columns[i].Name == "TenGoi")
                    {
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Columns[i].HeaderText = "Tên gọi";
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Columns[i].IsVisible = true;
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Columns[i].Width = 210; 
                    }
                    if (this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Columns[i].Name == "GhiChu")
                    {
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Columns[i].HeaderText = "Ghi chú";
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Columns[i].IsVisible = true;
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Columns[i].Width = 130; 
                    }
                    if (this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Columns[i].Name == "IsUsed")
                    {
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Columns[i].HeaderText = "Đã sử dụng";
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Columns[i].IsVisible = true;
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Columns[i].Width = 80; 
                    }
                }

                for (int i = 0; i < this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].ColumnCount; i++)
                {
                    this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Columns[i].IsVisible = false;
                    if (this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Columns[i].Name == "MaDanhMuc")
                    {
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Columns[i].HeaderText = "Mã Danh mục";
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Columns[i].IsVisible = true;
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Columns[i].Width = 80;
                    }
                    if (this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Columns[i].Name == "TenGoi")
                    {
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Columns[i].HeaderText = "Tên gọi";
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Columns[i].IsVisible = true;
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Columns[i].Width = 210;
                    }
                    if (this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Columns[i].Name == "GhiChu")
                    {
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Columns[i].HeaderText = "Ghi chú";
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Columns[i].IsVisible = true;
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Columns[i].Width = 130;
                    }
                    if (this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Columns[i].Name == "IsUsed")
                    {
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Columns[i].HeaderText = "Đã sử dụng";
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Columns[i].IsVisible = true;
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Columns[i].Width = 80;
                    }
                }
                for (int i = 0; i < this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates[0].ColumnCount; i++)
                {
                    this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates[0].Columns[i].IsVisible = false;
                    if (this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates[0].Columns[i].Name == "MaDanhMuc")
                    {
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates[0].Columns[i].HeaderText = "Mã Danh mục";
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates[0].Columns[i].IsVisible = true;
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates[0].Columns[i].Width = 80;
                    }
                    if (this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates[0].Columns[i].Name == "TenGoi")
                    {
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates[0].Columns[i].HeaderText = "Tên gọi";
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates[0].Columns[i].IsVisible = true;
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates[0].Columns[i].Width = 210;
                    }
                    if (this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates[0].Columns[i].Name == "GhiChu")
                    {
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates[0].Columns[i].HeaderText = "Ghi chú";
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates[0].Columns[i].IsVisible = true;
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates[0].Columns[i].Width = 130;
                    }
                    if (this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates[0].Columns[i].Name == "IsUsed")
                    {
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates[0].Columns[i].HeaderText = "Đã sử dụng";
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates[0].Columns[i].IsVisible = true;
                        this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates[0].Columns[i].Width = 80;
                    }
                }
                */
                #endregion
                // this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Columns["RowIDParent"].IsVisible = true;
                //this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Columns["RowIDParent"].Width = 80;

               // this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.GroupDescriptors.Add(descriptor);

               // this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.GroupDescriptors.Clear();
               //// this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.GroupDescriptors.Add(new GridGroupByExpression("RowIDParent as RowIDParent format \"{0}: {1}\" Group By RowIDParent")); 
               //this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.AutoGenerateHierarchy = true; 
               //this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.AutoExpandGroups = true;
                this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.ExpandAll();

                GridViewTemplate template = new GridViewTemplate();
                template.DataSource = controller.LoadData();//nwindDataSet.Products;
                this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates.Add(template);
                template.Columns["MaDanhMuc"].HeaderText = "Mã Danh mục";
                template.Columns["TenGoi"].HeaderText = "Tên gọi";
                template.Columns["GhiChu"].HeaderText = "Ghi chú";
                template.Columns["IsUsed"].HeaderText = "Đã sử dụng";
                for (int i = 0; i < this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].ColumnCount; i++)
                {
                    this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Columns[i].IsVisible = false;
                }
                template.Columns["MaDanhMuc"].IsVisible = true;
                template.Columns["MaDanhMuc"].Width = 80;
                template.Columns["TenGoi"].IsVisible = true;
                template.Columns["TenGoi"].Width = 210;
                template.Columns["GhiChu"].IsVisible = true;
                template.Columns["GhiChu"].Width = 130;
                template.Columns["IsUsed"].IsVisible = true;
                template.Columns["IsUsed"].Width = 80;
               

                GridViewRelation relation = new GridViewRelation(radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.MasterTemplate,template);
                relation.RelationName = "DanhMucHeThong";
                relation.ParentColumnNames.Add("RowID");
                relation.ChildColumnNames.Add("RowIDParent");
                this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Relations.Add(relation);


                GridViewTemplate template1 = new GridViewTemplate();
                template1.DataSource = controller.LoadData();//nwindDataSet.Products;
                //template1 = this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].ChildGridViewTemplates.Add(
                this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].ChildGridViewTemplates.Add(template1);
                template1.Columns["MaDanhMuc"].HeaderText = "Mã Danh mục";
                template1.Columns["TenGoi"].HeaderText = "Tên gọi";
                template1.Columns["GhiChu"].HeaderText = "Ghi chú";
                template1.Columns["IsUsed"].HeaderText = "Đã sử dụng";
                for (int i = 0; i < this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].ColumnCount; i++)
                {
                    this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0].Templates[0].Columns[i].IsVisible = false;
                }
                template1.Columns["MaDanhMuc"].IsVisible = true;
                template1.Columns["MaDanhMuc"].Width = 80;
                template1.Columns["TenGoi"].IsVisible = true;
                template1.Columns["TenGoi"].Width = 210;
                template1.Columns["GhiChu"].IsVisible = true;
                template1.Columns["GhiChu"].Width = 130;
                template1.Columns["IsUsed"].IsVisible = true;
                template1.Columns["IsUsed"].Width = 80;

                GridViewRelation relation1 = new GridViewRelation(radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Templates[0], template1);
                relation1.RelationName = "DanhMucHeThong";
                relation1.ParentColumnNames.Add("RowID");
                relation1.ChildColumnNames.Add("RowIDParent");
                this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.MasterTemplate.Relations.Add(relation1);



                this.radMultiColumnComboBox_DanhMucCha.AutoFilter = true;
                this.radMultiColumnComboBox_DanhMucCha.EditorControl.Templates[0].EnableHierarchyFiltering = true;
                //this.radMultiColumnComboBox_DanhMucCha.EditorControl.ShowFilteringRow =true;
                FilterDescriptor idFd = new FilterDescriptor("MaDanhMuc", FilterOperator.Contains, null);
                this.radMultiColumnComboBox_DanhMucCha.EditorControl.FilterDescriptors.Add(idFd);
                FilterDescriptor idNotes = new FilterDescriptor("GhiChu", FilterOperator.Contains, null);
                this.radMultiColumnComboBox_DanhMucCha.EditorControl.FilterDescriptors.Add(idNotes);
                FilterDescriptor companyNameFd = new FilterDescriptor("TenGoi", FilterOperator.Contains, null);
                this.radMultiColumnComboBox_DanhMucCha.EditorControl.FilterDescriptors.Add(companyNameFd);
                //this.radMultiColumnComboBox_DanhMucCha.EditorControl.Templates[0].FilterDescriptors.Add(companyNameFd);
                this.radMultiColumnComboBox_DanhMucCha.EditorControl.FilterDescriptors.LogicalOperator = FilterLogicalOperator.Or;
                // this.radMultiColumnComboBox_DanhMucCha.EditorControl.Templates[0].FilterDescriptors.LogicalOperator = FilterLogicalOperator.Or;

                //this.radMultiColumnComboBox_DanhMucCha.AutoFilter = true;
                //this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.EnableFiltering = true;
               ////this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.Templates[0].EnableFiltering = true;
                ////this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.Templates[0].Templates[0].EnableFiltering = true;
                //this.radMultiColumnComboBox_DanhMucCha.DisplayMember = "TenGoi";
                //FilterDescriptor filter = new FilterDescriptor();
                //filter.PropertyName = this.radMultiColumnComboBox_DanhMucCha.DisplayMember;
                //filter.Operator = FilterOperator.Contains;
                //this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.FilterDescriptors.Add(filter);
                //this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.Templates[0].FilterDescriptors.Add(filter);

                //this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.Templates[0].ExpandAll(); // for expand
                //this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.Templates[0].CollapseAllGroups(); // for collapse
                HideWhenClickTreeViewLevel(Level);
            } 
            #endregion

        }
        void HideWhenClickTreeViewLevel(int Level)
        {
            if (Level != -1)
            {
                if (Level == 0)
                {
                    this.radMultiColumnComboBox_DanhMucCha.Enabled = false;
                }
                else
                {
                    this.radMultiColumnComboBox_DanhMucCha.Enabled = true;
                    if (Level == 1)
                    {
                        if (this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.Templates.Count > 0)
                        {
                            for (int i = 0; i < this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.Templates[0].Rows.Count(); i++)
                            {
                                this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.Templates[0].Rows[i].IsVisible = false;
                            }
                            if (this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.Templates[0].Templates.Count() > 0)
                            {
                                for (int i = 0; i < this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.Templates[0].Templates[0].Rows.Count(); i++)
                                {
                                    this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.Templates[0].Templates[0].Rows[i].IsVisible = false;
                                }
                                if (this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates.Count > 0)
                                {
                                    for (int i = 0; i < this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates[0].Rows.Count(); i++)
                                    {
                                        this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates[0].Rows[i].IsVisible = false;
                                    }
                                }
                            }
                        }


                    }
                    else if (Level == 2)
                    {
                        if (this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.Templates[0].Templates.Count() > 0)
                        {
                            for (int i = 0; i < this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.Templates[0].Templates[0].Rows.Count(); i++)
                            {
                                this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.Templates[0].Templates[0].Rows[i].IsVisible = false;
                            }
                            if (this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates.Count > 0)
                            {
                                for (int i = 0; i < this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates[0].Rows.Count(); i++)
                                {
                                    this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates[0].Rows[i].IsVisible = false;
                                }
                            }
                        }

                    }
                    else if (Level == 3)
                    {
                        if (this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates.Count > 0)
                        {
                            for (int i = 0; i < this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates[0].Rows.Count(); i++)
                            {
                                this.radMultiColumnComboBox_DanhMucCha.EditorControl.MasterTemplate.Templates[0].Templates[0].Templates[0].Rows[i].IsVisible = false;
                            }
                        }
                    }
                }
            }
        }
        private void LoadDM_HeThong()
        {
            radTreeView_DanhMuc.Nodes.Clear();
            foreach (tbl_DanhMuc_HeThong item in controller.LoadDataParent())
            {
                RadTreeNode node1 = new RadTreeNode(item.TenGoi);
                node1.Tag = item.RowID.ToString();
                //node1.Font = new Font("Segoe UI", FontStyle.Bold);
                radTreeView_DanhMuc.Nodes.Add(LoadDM_Child(item.RowID, node1));
                radTreeView_DanhMuc.ExpandAll();
            }
        }
        private RadTreeNode LoadDM_Child(int parenID, RadTreeNode trn)
        {
            foreach (tbl_DanhMuc_HeThong itemcon in controller.LoadDataChild(parenID))
            {
                RadTreeNode trcon = new RadTreeNode(itemcon.TenGoi);
                trcon.Tag = itemcon.RowID.ToString();
                trn.Nodes.Add(trcon);
                if (controller.LoadDataChild(itemcon.RowID).Count > 0)
                {
                    LoadDM_Child(itemcon.RowID, trcon);
                }
            }
            return trn;
        }
        private List<tbl_DanhMuc_HeThong> LoadDM_HeThongCombobox()
        {
            List<tbl_DanhMuc_HeThong> lst = new List<tbl_DanhMuc_HeThong>();
            foreach (tbl_DanhMuc_HeThong item in controller.LoadDataParent())
            {
                item.TenGoi = item.TenGoi;
                lst.Add(item);
                LoadDM_ChildCombobox(item.RowID,lst,"----");
            }
            return lst;
            //clearcheck();
        }
        private void LoadDM_ChildCombobox(int parenID, List<tbl_DanhMuc_HeThong> lst,string ADD)
        {
            foreach (tbl_DanhMuc_HeThong itemcon in controller.LoadDataChild(parenID))
            {
                itemcon.TenGoi = ADD + itemcon.TenGoi;
                lst.Add(itemcon);
                if (controller.LoadDataChild(itemcon.RowID).Count > 0)
                {
                    LoadDM_ChildCombobox(itemcon.RowID, lst,ADD + "----");
                }
            }
        }
        private void radTreeView_DanhMuc_SelectedNodeChanged(object sender, RadTreeViewEventArgs e)
        {
            if (radTreeView_DanhMuc.SelectedNodes.Count > 0)
            {
                SetupTemplates("", e.Node.Level);
                int ID = int.Parse(radTreeView_DanhMuc.SelectedNode.Tag.ToString());
                tbl_DanhMuc_HeThong dm = controller.LoadDM_HeThuong(ID);
                radTextBox_MaDM.Name = dm.RowID.ToString();
                radTextBox_MaDM.Text = dm.MaDanhMuc;
                radTextBox_TenGoi.Text = dm.TenGoi;
                if (dm.ThuTu.HasValue)
                {
                    numericUpDown_OrderBy.Value = dm.ThuTu.Value;
                }
                if (dm.RowIDParent != null)
                {
                    GridViewHierarchyRowInfo row = this.radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.EditorControl.Templates[0].Rows.Cast<GridViewHierarchyRowInfo>()
               .Where(p => p.Cells["RowID"].Value != null).Where(r => r.Cells["RowID"].Value.ToString() == dm.RowIDParent.Value.ToString()).FirstOrDefault();
                    radMultiColumnComboBox_DanhMucCha.SelectedItem = row;
                }
                
                radTextBox4.Text = dm.GhiChu;
                radCheckBox_IsUsed.Checked =dm.IsUsed != null ? dm.IsUsed.Value : false ;
            }
        }

        private void radMenuItem_LuuLai_Click(object sender, EventArgs e)
        {
            tbl_DanhMuc_HeThong dm = new tbl_DanhMuc_HeThong();
            if (radTextBox_MaDM.Name != "radTextBox_MaDM")
            {
                int ID = int.Parse(radTextBox_MaDM.Name);
                dm = controller.LoadDM_HeThuong(ID);
                dm.TenGoi = radTextBox_TenGoi.Text;
                dm.RowIDParent = int.Parse(radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.SelectedValue.ToString());
                if (dm.RowID != dm.RowIDParent)
                {
                    dm.GhiChu = radTextBox4.Text;
                    dm.IsUsed = radCheckBox_IsUsed.Checked;
                    if (numericUpDown_OrderBy.Value.ToString() != "")
                    {
                        dm.ThuTu = int.Parse(numericUpDown_OrderBy.Value.ToString());
                    }
                    controller.Save(false, dm);
                }
                else
                { MessageBox.Show("Không thể chọn danh mục " + dm.TenGoi + " là cha của chính nó." , "Xóa Danh mục", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
            }
            else
            {
                dm.MaDanhMuc = radTextBox_MaDM.Text;
                dm.TenGoi = radTextBox_TenGoi.Text;
                if (radMultiColumnComboBox_DanhMucCha.Text != "----- Chọn Danh mục cha -----")
                {
                    dm.RowIDParent = int.Parse(radMultiColumnComboBox_DanhMucCha.MultiColumnComboBoxElement.SelectedValue.ToString());
                }
                else
                {
                    dm.RowIDParent = null;
                }
                dm.GhiChu = radTextBox4.Text;
                dm.IsUsed = radCheckBox_IsUsed.Checked;
                if (numericUpDown_OrderBy.Value.ToString() != "")
                {
                    dm.ThuTu =int.Parse(numericUpDown_OrderBy.Value.ToString());
                }
                controller.Save(true,dm);
            }
            LoadDM_HeThong();
        }

        private void radMenuItem_TaoMoi_Click(object sender, EventArgs e)
        {
            LoadRefresh();
        }

        private void radMenuItem_Refresh_Click(object sender, EventArgs e)
        {
            radForm_CRM_DanhSachBaoGia_Load(sender, e);
        }

        void LoadRefresh()
        {
            radMultiColumnComboBox_DanhMucCha.Enabled = true;
            radTextBox_MaDM.Name = "radTextBox_MaDM";
            radTextBox_MaDM.ReadOnly = false;
            numericUpDown_OrderBy.Value = 1;
            radTextBox_MaDM.Text = "";
            radMultiColumnComboBox_DanhMucCha.Text = "----- Chọn Danh mục cha -----";
            radTextBox_TenGoi.Text = "";
            radTextBox4.Text = "";
        }
        private void radMenuItem_XoaBo_Click(object sender, EventArgs e)
        {
            if (radTextBox_MaDM.Text != "")
            {
                int ID = int.Parse(radTreeView_DanhMuc.SelectedNode.Tag.ToString());
                tbl_DanhMuc_HeThong dm = controller.LoadDM_HeThuong(ID);
                if (MessageBox.Show("Danh mục " + dm.TenGoi + " và các Danh mục con sẽ bị xóa." + Environment.NewLine + "Xin xác nhận lại việc có tiếp tục xóa hay không?", "Xóa Danh mục", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (dm != null)
                    {
                        foreach (tbl_DanhMuc_HeThong itemcon in controller.LoadDataChild(dm.RowID))
                        {
                            if (controller.LoadDataChild(itemcon.RowID).Count > 0)
                            {
                                DeleteChildRow(itemcon.RowID);
                            }
                        }
                        string _mess = "";
                        controller.DeleteObject(dm, out _mess);
                        RadDesktopAlert_SavingStatus.ShowAlertDeleteSuccess("Thông tin Danh mục", "Đã xóa thành công danh mục hệ thống!");
                        radMenuItem_Refresh_Click(sender, e);
                    }
                }
            }
        }
        void DeleteChildRow(int ParentID)
        {
            foreach (tbl_DanhMuc_HeThong itemcon in controller.LoadDataChild(ParentID))
            {
                if (controller.LoadDataChild(itemcon.RowID).Count > 0)
                {
                    DeleteChildRow(itemcon.RowID);
                }
                string _mess = "";
                controller.DeleteObject(itemcon,out _mess);
            }
        }

    }
}
